//
//  RABookMallController.m
//  ReadApp
//
//  Created by 飞猪 on 2018/11/4.
//  Copyright © 2018年 Facebook. All rights reserved.
//

#import "RABookMallController.h"
#import "RDNetworkTool.h"
#import "JZPageController.h"
#import "RABookMallDetailController.h"
#import "RCTEntryViewController.h"

@interface RABookMallController ()

@property (nonatomic, strong) JZPageController *pageController;

@end

@implementation RABookMallController

#pragma mark - LifeCycle
- (void)viewDidLoad {
    [super viewDidLoad];
  
  self.view.backgroundColor = [UIColor whiteColor];
  
  self.automaticallyAdjustsScrollViewInsets = NO;
  
  [self displayView];
  
  [self setupSearch];
}

- (void)viewWillAppear:(BOOL)animated {
  [super viewWillAppear:animated];
  
  [self.navigationController setNavigationBarHidden:YES animated:NO];
  
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated {
  [super viewWillDisappear:animated];
  
  [self.navigationController setNavigationBarHidden:NO animated:YES];
  
  [UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
}

- (void)displayView {
  
  NSArray *categorys = @[@"精选", @"女频", @"男频"];
  
  NSMutableArray *ctrls = [NSMutableArray array];
  for (NSInteger index = 0; index < categorys.count; ++index) {
    
    RABookMallDetailController *bookMallDetailController = [[RABookMallDetailController alloc] init];
    bookMallDetailController.type = index;
    [ctrls addObject:bookMallDetailController];
    [self addChildViewController:bookMallDetailController];
    
  }
  
  JZPageControllerConfig *config = [[JZPageControllerConfig alloc] init];
  config.categoryType = JZPageControllerCategoryTypeAverage;
  config.titleColor = UIColorHex(666666);
  config.titleSelectedColor = kMainColor;
  config.indicatorLineColor = kMainColor;
  
  CGRect rect = CGRectMake(0, [UIApplication sharedApplication].statusBarFrame.size.height, self.view.bounds.size.width, kScreenHeight - self.tabBarController.tabBar.size.height - [UIApplication sharedApplication].statusBarFrame.size.height);
  JZPageController *pageController = [[JZPageController alloc] initWithFrame:rect
                                                                      config:config
                                                                      titles:categorys
                                                             viewControllers:ctrls.copy];
  self.pageController = pageController;
  [self.view addSubview:pageController];
}

- (void)setupSearch {
  JZPageCategoryView *categoryV = self.pageController.pageCategoryView;
  
  UIButton *searchB = [UIButton buttonWithType:UIButtonTypeCustom];
  [categoryV addSubview:searchB];
  [searchB addTarget:self action:@selector(rightBarClick) forControlEvents:UIControlEventTouchUpInside];
  [searchB setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
  searchB.frame = CGRectMake(kScreenWidth - CGRectGetHeight(categoryV.frame) - 8, 0, CGRectGetHeight(categoryV.frame), CGRectGetHeight(categoryV.frame));
}

- (void)rightBarClick {
  NSLog(@"rightBarClick");
  RCTEntryViewController *ctrl = [[RCTEntryViewController alloc] init];
  ctrl.router = @"SearchList";
  [self.navigationController pushViewController:ctrl animated:YES];
}

@end
